
import UIKit

public extension UIViewController {

    func showAlert(title: String? = nil, message: String? = nil, okTitle: String? = "确认", noTitle: String? = "取消", completion: ((_ isOK: Bool) -> ())? = nil) {
        
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        if let okTitle = okTitle {
            let yesAction = UIAlertAction(title: okTitle, style: .default) { (action) in
                completion?(true)
            }
            alert.addAction(yesAction)
        }
        
        if let noTitle = noTitle {
            let noAction = UIAlertAction(title: noTitle, style: .cancel) { (action) in
                completion?(false)
            }
            alert.addAction(noAction)
        }
        self.present(alert, animated: true, completion: nil)
    }
    
    func showActionSheets(title: String? = nil, message: String? = nil, actions: [String] = [], okTitle: String? = nil, noTitle: String = "取消", completion: ((_ title: String?) -> ())? = nil) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet)
        
        for actionTitle in actions {
            let ac = UIAlertAction(title: actionTitle, style: .default) { (action) in
                completion?(action.title)
            }
            alert.addAction(ac)
        }
        
        if let okTitle = okTitle {
            let yesAction = UIAlertAction(title: okTitle, style: .destructive) { (action) in
                completion?(action.title)
            }
            alert.addAction(yesAction)
        }
        
        let cancelAction = UIAlertAction(title: noTitle, style: .cancel) { (action) in
            completion?(action.title)
        }
        alert.addAction(cancelAction)
        
        self.present(alert, animated: true, completion: nil)
    }
    
    func hideKeyboardWhenTappedAround() {
        let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
    
    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
    
    /// draw a red cycle on tabbar item
    func setTabBarBadgeVisible(visible:Bool, index: Int? = nil) {
        
        var tabController: UITabBarController?
        
        if self is UITabBarController {
            tabController = self as? UITabBarController
        } else {
            tabController = self.tabBarController
        }
        
        guard let tabBarController = tabController else { return }
        
        let indexFinal:Int
        if (index != nil) {
            
            indexFinal = index!
            
        } else {
            let index3 = tabBarController.viewControllers?.index(of: self)
            if index3 == nil {
                return;
            } else {
                indexFinal = index3!
            }
        }
        
        guard let barItems = tabBarController.tabBar.items else { return }
        
        let tag = 8888
        var tabBarItemView:UIView?
        
        for subview in tabBarController.tabBar.subviews {
            
            let className = String(describing: type(of: subview))
            
            guard className == "UITabBarButton" else {
                continue
            }
            
            var label:UILabel?
            var dotView:UIView?
            
            for subview2 in subview.subviews {
                
                if subview2.tag == tag {
                    dotView = subview2;
                }
                else if (subview2 is UILabel)
                {
                    label = subview2 as? UILabel
                }
            }
            
            if label?.text == barItems[indexFinal].title
            {
                dotView?.removeFromSuperview()
                tabBarItemView = subview;
                break;
            }
        }
        
        if (tabBarItemView == nil || !visible) { return }
        
        let barItemWidth = tabBarItemView!.bounds.width
        
        let x = barItemWidth * 0.5 + (barItems[indexFinal].selectedImage?.size.width ?? barItemWidth) / 2
        let y:CGFloat = 5
        let size:CGFloat = 10;
        
        let redDot = UIView(frame: CGRect(x: x, y: y, width: size, height: size))
        
        redDot.tag = tag
        redDot.backgroundColor = UIColor.red
        redDot.layer.cornerRadius = size/2
        
        
        tabBarItemView!.addSubview(redDot)
    }
}
